package com.chiemy.cardview;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;

import com.chiemy.cardview.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Harry on 2018/5/9.
 */

public class SettingActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
    private static final String TAG = SettingActivity.class.getSimpleName();

    private Spinner mRssiSpinner;
    private EditText device_name;
    private Switch switch1;
    private boolean auto;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        //设置信号强度
        mRssiSpinner = (Spinner) findViewById(R.id.rssi_spinner);
        ArrayAdapter<Integer> RssiArrayAdapter = new ArrayAdapter<Integer>(this,
                android.R.layout.simple_spinner_item, rssiList);
        RssiArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mRssiSpinner.setAdapter(RssiArrayAdapter);

        mRssiSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Log.i(TAG, "i:  " + i);
                int rssi = rssiList.get(i);
                Log.i(TAG, " rssi.value;" + rssi);
                SPUtils.putInt(SettingActivity.this, SPUtils.SET_RSSI, rssi);
//                AppApplication.setRssi = rssi;



            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        switch1 = (Switch) findViewById(R.id.switch1);
        device_name = (EditText) findViewById(R.id.device_name);

        switch1.setOnCheckedChangeListener(this);


    }

    List<Integer> rssiList = new ArrayList<Integer>() {
        {
            add(-45);
            add(-50);
            add(-55);
            add(-60);
            add(-65);
            add(-70);
            add(-75);
            add(-80);
            add(-85);
            add(-90);
            add(-95);
            add(-100);
            add(-105);
        }

    };

    @Override
    protected void onResume() {
        super.onResume();
        //设置选中的位置
        int rssi = SPUtils.getInt(SettingActivity.this, SPUtils.SET_RSSI, -55);
        mRssiSpinner.setSelection(rssiList.indexOf(rssi));


        auto = SPUtils.getBoolean(SettingActivity.this, SPUtils.AUTO_OTA, false);


        switch1.setChecked(auto);

        device_name.setVisibility(auto ?View.VISIBLE:View.INVISIBLE);


        String deviceName = SPUtils.getString(SettingActivity.this, SPUtils.DEVICE_NAME, "");
        device_name.setText(deviceName);
        device_name.setSelection(deviceName.length());

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        auto = isChecked;
        device_name.setVisibility(isChecked?View.VISIBLE:View.INVISIBLE);
        SPUtils.putBoolean(SettingActivity.this,SPUtils.AUTO_OTA,auto);

//        if (!auto){
//            SPUtils.putString(SettingActivity.this,SPUtils.DEVICE_NAME,"");
//        }

        String deviceName = SPUtils.getString(SettingActivity.this, SPUtils.DEVICE_NAME, "");
        if (auto){
            device_name.setText(deviceName);
            device_name.setSelection(deviceName.length());

        }


    }

    @Override
    public void onBackPressed() {
        if (auto) {
            String deviceName = device_name.getText().toString();
            if (TextUtils.isEmpty(deviceName)) {
                Toast.makeText(SettingActivity.this, "请输入设备名称！", Toast.LENGTH_SHORT).show();

            } else {
                SPUtils.putString(SettingActivity.this, SPUtils.DEVICE_NAME, deviceName);

                super.onBackPressed();
                return;
            }
        }else{
            super.onBackPressed();
            return;
        }
    }



}
